PowerTCP Mail for .NET
MailMessage Constructor(String,String,FileInfo[])
Example 




Used to initialize Multipart.Text (Parts[0]). If null or empty no Textpart is created.
Used to initialize Multipart.Html (Parts[1]). If null or empty an ArgumentNullException is thrown.
Used to initialized a "multipart/related" part (Parts[1]) that provides linked embedded resources referenced by Multipart.Html.
Initializes Parts with new Textpart, Htmlpart and linked Resource parts.
Syntax
Public Function New( _
   ByVal text As String, _
   ByVal html As String, _
   ByVal htmlResources() As FileInfo _
)
Dim text As String
Dim html As String
Dim htmlResources() As FileInfo
 
Dim instance As New MailMessage(text, html, htmlResources)
public MailMessage( 
   string text,
   string html,
   FileInfo[] htmlResources
)
public: MailMessage( 
   string* text,
   string* html,
   FileInfo*[]* htmlResources
)
public:
MailMessage( 
   String^ text,
   String^ html,
   array<FileInfo^>^ htmlResources
)

Parameters

text
Used to initialize Multipart.Text (Parts[0]). If null or empty no Textpart is created.
html
Used to initialize Multipart.Html (Parts[1]). If null or empty an ArgumentNullException is thrown.
htmlResources
Used to initialized a "multipart/related" part (Parts[1]) that provides linked embedded resources referenced by Multipart.Html.
Exceptions
ExceptionDescription
System.ArgumentExceptionhtmlResources missing reference to an HTML link.
System.ArgumentNullExceptionhtml cannot be null or empty.
Remarks

This constructor is used to create HTML email. "multipart/alternative" and/or "multipart/related" parts are created to represent the parameters provided.

If text is null or empty, then html and htmlResources are used to create either a "text/html" part or a "multipart/related" part that contains a "text/html" part. Otherwise, Part.ContentType is set to "multipart/alternative" and 2 parts are created and saved in Parts.

If htmlResources is null, then html is represented as a single Htmlpart. Otherwise, a new "multipart/related" Multipart is created and initialized with html and linked htmlResources.

Example
This example demonstrates creating a new MailMessage and populating it with HTML that references local files, such as a "Webpage, complete (*.htm;*.html)" as saved by Internet Explorer.
private MailMessage getMessageFromHtml(string pathToHtml, string pathToHtmlResources, string from, string to, string subject, bool includeTextPlain)
{
    //Note: to load a .mht (Mime HTML) file, use MailMessage.Open() instead.

    //Load HTML
    string html;
    using (StreamReader sr = new StreamReader(pathToHtml))
        html = sr.ReadToEnd();

    //Initialize a MailMessage object. includeTextPlain controls whether the message should include a text/plain part 
    //(which determines whether the message is multipart/alternative (when true) or multipart/related (when false))
    string textPlain = (includeTextPlain) ? "text/plain representation of included HTML" : null;
    MailMessage mailMsg = new MailMessage(textPlain, html, new DirectoryInfo(pathToHtmlResources).GetFiles());

    //Set sender, recipients and subject
    mailMsg.From = from;
    mailMsg.To = to;
    mailMsg.Subject = subject;
    return mailMsg;
}
Private Function getMessageFromHtml(ByVal pathToHtml As String, ByVal fromAddress As String, ByVal toAddress As String, ByVal subject As String) As MailMessage
     'Note: to load a .mht (Mime HTML) file, use MailMessage.Open() instead.

     'Load HTML
     Dim sr As New StreamReader(pathToHtml)
     Dim html As String = sr.ReadToEnd()
     sr.Close()

     'Initialize a MailMessage object. includeTextPlain controls whether the message should include a 
     'text/plain part (which determines whether the message is multipart/alternative (when true) or multipart/related (when false))
     Dim textPlain As String = If(includeTextPlain, "text/plain representation of included HTML", Nothing)
     Dim mailMsg As New MailMessage(textPlain, html, New DirectoryInfo(pathToHtmlResources).GetFiles())

     'Set sender, recipients and subject
     mailMsg.From = fromAddress
     mailMsg.To = toAddress
     mailMsg.Subject = subject
     Return mailMsg
End Function
See Also

Reference

MailMessage Class
MailMessage Members
Overload List


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic